package Action;

import Entities.DoeBookEntity;
import Service.SearchService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;

import java.util.List;
import java.util.Map;

//控制搜索的Action（在搜索页面展示搜索结果），同时包括返回主页的功能
public class SearchAction extends ActionSupport
{
    private String searchContent;  //搜索内容
    private SearchService searchService;
    private List<DoeBookEntity> searchList;
    private int initLimit = 14;
    private String after_id = "0";
    private String limit=String.valueOf(initLimit);
    private List<DoeBookEntity> feedList;
    private int searchResSize = 0;



    public int getSearchResSize() {
        return searchResSize;
    }

    public void setSearchResSize(int searchResSize) {
        this.searchResSize = searchResSize;
    }

    public List<DoeBookEntity> getSearchList() {
        return searchList;
    }

    public void setSearchList(List<DoeBookEntity> searchList) {
        this.searchList = searchList;
    }

    public String getSearchContent() {
        return searchContent;
    }

    public void setSearchContent(String searchContent) {
        this.searchContent = searchContent;
    }

    public SearchService getSearchService() {
        return searchService;
    }

    public void setSearchService(SearchService searchService) {
        this.searchService = searchService;
    }

    public int getInitLimit() {
        return initLimit;
    }

    public void setInitLimit(int initLimit) {
        this.initLimit = initLimit;
    }

    public String getAfter_id() {
        return after_id;
    }

    public void setAfter_id(String after_id) {
        this.after_id = after_id;
    }

    public String getLimit() {
        return limit;
    }

    public void setLimit(String limit) {
        this.limit = limit;
    }

    public List<DoeBookEntity> getFeedList() {
        return feedList;
    }

    public void setFeedList(List<DoeBookEntity> feedList) {
        this.feedList = feedList;
    }

    public String execute()
    {
        String param_after_id = ServletActionContext.getRequest().getParameter("after_id");
        String param_limit = ServletActionContext.getRequest().getParameter("limit");
        setAfter_id(param_after_id != null ? param_after_id : "0");
        setLimit(param_limit != null ? param_limit : String.valueOf(initLimit));
        Map<String, Object> session =  ActionContext.getContext().getSession();
        String user_name = (String) session.get("user_name");  //获取登录或注册时存入session的参数——用户名


        /*
        贼鸡儿难受这个编码问题
         */
        //TODO

        /*
        if (searchContent != null) {
            try {
                setSearchContent(new String(searchContent.getBytes("iso-8859-1"), "utf-8"));
            } catch (Exception e) {
                e.printStackTrace();
            }
            session.put("searchContent", searchContent);
        }
        else
            searchContent = (String) session.get("searchContent");
           */

        String session_Id = ServletActionContext.getRequest().getSession().getId();

        String result;
        if (Integer.parseInt(after_id) >= initLimit)
        {
            setFeedList(searchService.search(searchContent, user_name, Integer.parseInt(after_id),Integer.parseInt(limit), session_Id,"searchResult"));
            result = "append";
        }
        else
        {
            setSearchList(searchService.search(searchContent, user_name, Integer.parseInt(after_id),Integer.parseInt(limit),session_Id,"searchResult"));
            result = "success";
        }
        setSearchResSize((int)session.get("searchResult_"+searchContent));
        return result;
    }

    public String getBookListByTag(){
        String param_after_id = ServletActionContext.getRequest().getParameter("after_id");
        String param_limit = ServletActionContext.getRequest().getParameter("limit");
        setAfter_id(param_after_id != null ? param_after_id : "0");
        setLimit(param_limit != null ? param_limit : String.valueOf(initLimit));
        Map<String, Object> session =  ActionContext.getContext().getSession();
        String user_name = (String) session.get("user_name");  //获取登录或注册时存入session的参数——用户名
        String session_Id = ServletActionContext.getRequest().getSession().getId();
        String result;


        if (searchContent != null) {
            try {
                setSearchContent(new String(searchContent.getBytes("iso-8859-1"), "utf-8"));
            } catch (Exception e) {
                e.printStackTrace();
            }
            session.put("searchContentByTag", searchContent);
        }
        else
            searchContent = (String) session.get("searchContentByTag");

        if (Integer.parseInt(after_id) >= initLimit)
        {
            setFeedList(searchService.search(searchContent, user_name, Integer.parseInt(after_id),Integer.parseInt(limit),session_Id,"searchResultByTag"));
            result = "append";
        }
        else
        {
            setSearchList(searchService.search(searchContent, user_name, Integer.parseInt(after_id),Integer.parseInt(limit),session_Id,"searchResultByTag"));
            result = "success";
        }
        setSearchResSize((int)session.get("searchResultByTag_"+searchContent));
        return result;


    }
}
